Completed
Push — master ( 4fcad4...efcce7 )
by Muhammad Dyas
19s queued 16s
created

CommandHandler.parseSlashCommand   A

Complexity

Conditions 3

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
1
import NewPollFormCard from '../cards/NewPollFormCard';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import BaseHandler from './BaseHandler';
4
import {buildOptionsFromMessage} from '../helpers/utils';
5
6
export default class CommandHandler extends BaseHandler {
7
  private slashCommand: chatV1.Schema$SlashCommandMetadata | undefined;
8
9
  public constructor(event: chatV1.Schema$DeprecatedEvent) {
10
    super(event);
11
    this.parseSlashCommand();
12
13
    if (this.slashCommand === undefined) {
14
      throw new Error('No Slash Command found');
15
    }
16
  }
17
18
  parseSlashCommand() {
19
    this.getAnnotations().forEach((annotation) => {
20
      if (annotation.type === 'SLASH_COMMAND') {
21
        this.slashCommand = annotation.slashCommand!;
22
      }
23
    });
24
  }
25
26
  process(): chatV1.Schema$Message {
27
    switch (this.slashCommand!.commandName) {
28
      case '/poll':
29
        const argumentText = this.event.message?.argumentText?.trim() ?? '';
30
        const options = buildOptionsFromMessage(argumentText);
31
        return {
32
          actionResponse: {
33
            type: 'DIALOG',
34
            dialogAction: {
35
              dialog: {
36
                body: new NewPollFormCard(options).create(),
37
              },
38
            },
39
          },
40
        };
41
      default:
42
        return {
43
          thread: this.event.message!.thread,
44
          actionResponse: {
45
            type: 'NEW_MESSAGE',
46
          },
47
          text: 'Hi there! I can help you create polls to enhance collaboration and efficiency ' +
48
            'in decision-making using Google Chat™.\n' +
49
            '\n' +
50
            'Below is an example commands:\n' +
51
            '`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
52
            '`/poll "Which is the best country to visit" "Indonesia"` - to create a poll with ' +
53
            '"Which is the best country to visit" as the topic and "Indonesia" as the answer\n' +
54
            '\n' +
55
            'We hope you find our service useful and please don\'t hesitate to contact us ' +
56
            'if you have any questions or concerns.',
57
        };
58
    }
59
  }
60
}
61